package com.abe.abetest.file;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;

import com.abe.abetest.R;
import com.abe.libbase.base.BaseActivity;
import com.abe.libcore.ConstantCore;
import com.abe.libcore.base.permission.PermissionUtils;
import com.leon.lfilepickerlibrary.LFilePicker;
import com.yanzhenjie.permission.runtime.Permission;

import java.util.ArrayList;
import java.util.List;

public class FilePickerHelper extends BaseActivity {
    private int REQUEST_CODE_FROM_ACTIVITY = 1000;
    public static final String filePath = "filePath";

    @Override
    public void init() {
        PermissionUtils.INSTANCE.request(this, new PermissionUtils.PermissionCallback() {
            @Override
            public void onSuccess() {
                new LFilePicker()
                        .withActivity(FilePickerHelper.this)
                        .withRequestCode(REQUEST_CODE_FROM_ACTIVITY)
                        .withStartPath(ConstantCore.INSTANCE.getSD_PATH())//指定初始显示路径
                        .withIsGreater(false)//过滤文件大小 小于指定大小的文件
                        .withFileSize(500 * 1024)//指定文件大小为500K
                        .start();
            }
        }, Permission.Group.STORAGE);
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_helper;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        FilePath path = new FilePath(null, new ArrayList<>());
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_CODE_FROM_ACTIVITY) {
                //如果是文件选择模式，需要获取选择的所有文件的路径集合
                List<String> files = data.getStringArrayListExtra("paths");
                //如果是文件夹选择模式，需要获取选择的文件夹路径
                String file = data.getStringExtra("path");
                path = new FilePath(file, files);
            }
        }
        Intent back = new Intent();
        Bundle bundle = new Bundle();
        bundle.putParcelable(filePath, path);
        back.putExtras(bundle);
        setResult(RESULT_OK, back);
        finish();
    }

    public static class FilePath implements Parcelable {
        private String path;
        private List<String> paths;

        public FilePath(String path, List<String> paths) {
            this.path = path;
            this.paths = paths;
        }

        public String getPath() {
            return path;
        }

        public void setPath(String path) {
            this.path = path;
        }

        public List<String> getPaths() {
            return paths;
        }

        public void setPaths(List<String> paths) {
            this.paths = paths;
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {

            dest.writeString(path);
            dest.writeStringList(paths);
        }


        protected FilePath(Parcel in) {
            path = in.readString();
            paths = in.createStringArrayList();
        }

        public static final Creator<FilePath> CREATOR = new Creator<FilePath>() {
            @Override
            public FilePath createFromParcel(Parcel in) {
                return new FilePath(in);
            }

            @Override
            public FilePath[] newArray(int size) {
                return new FilePath[size];
            }
        };
    }
}
